package com.backend.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.backend.system.beans.log.LogVO;
import com.backend.system.common.RequestHeaderUserInfo;
import com.backend.system.common.RequestHolder;
import com.backend.system.dao.entity.OperateLog;
import com.backend.system.dao.mapper.OperateLogMapper;
import com.backend.system.service.IOperateLogService;
import com.backend.system.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author backend-manage
 * @since 2024-04-07
 */
@Slf4j
@Service
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements IOperateLogService {

    @Override
    public void addLog(String logDesc, String action, String queryId, String model, JSONObject ext) {
        RequestHeaderUserInfo userInfo = RequestHolder.getUserInfo();
        if (userInfo == null) {
            userInfo = new RequestHeaderUserInfo();
            userInfo.setLoginId(0L);
            userInfo.setUsername("system");
        }
        log.info("增加日志：userInfo:{},logDesc:{},action:{},queryId:{},model:{},ext:{}", userInfo, logDesc, action, queryId, model, ext);

        OperateLog operateLog = new OperateLog();
        // Hutool 的雪花算法设置logId
        Snowflake snowflake = IdUtil.getSnowflake();
        operateLog.setLogId(snowflake.nextId());
        operateLog.setContext(logDesc);
        operateLog.setQueryId(queryId);
        operateLog.setModule(model);
        operateLog.setLogType(action);
        if (ext != null) {
            operateLog.setExt(JSONObject.toJSONString(ext));
        }
        operateLog.setCreateUser(userInfo.getUsername());
        operateLog.setCreateUserId(userInfo.getLoginId() + "");
        operateLog.setCreateTime(LocalDateTime.now());
        this.save(operateLog);
    }

    @Override
    public List<LogVO> queryByQueryId(String queryId) {
        if (StringUtil.isBlank(queryId)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<OperateLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperateLog::getQueryId, queryId);
        queryWrapper.orderByDesc(OperateLog::getCreateTime);
        List<OperateLog> operateLogs = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(operateLogs)) {
            return new ArrayList<>();
        }
        return operateLogs.stream().map(this::buildVO).collect(Collectors.toList());
    }

    private LogVO buildVO(OperateLog log) {
        return buildVO(log, true);
    }

    private LogVO buildVO(OperateLog log, boolean buildExt) {
        LogVO vo = BeanUtil.toBean(log, LogVO.class);
        if (vo == null) {
            return null;
        }
        if (buildExt && log.getExt() != null) {
            vo.setExt(JSONObject.parseObject(log.getExt()));
        }
        return vo;
    }
}
